package com.ysz.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ysz.gulimall.product.entity.AttrEntity;
import com.ysz.gulimall.product.service.AttrAttrgroupRelationService;
import com.ysz.gulimall.product.service.AttrService;
import com.ysz.gulimall.product.service.CategoryService;
import com.ysz.gulimall.product.vo.AttrGroupRelationVo;
import com.ysz.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ysz.gulimall.product.entity.AttrGroupEntity;
import com.ysz.gulimall.product.service.AttrGroupService;
import com.ysz.common.utils.PageUtils;
import com.ysz.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author yangshengzhi
 * @email 2069784803@qq.com
 * @date 2023-09-14 15:53:35
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrAttrgroupRelationService relationService;
    @Resource
    private AttrService attrService;

    /**
     * @description:  属性分组关联信息
     * @param: [attrGroupId]
     * @return: com.ysz.common.utils.R
     * @author Yang
     * @date: 2023/9/25 11:06
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data",attrEntityList);
    }
    //product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrWithAttr(@PathVariable Long catelogId){
        //查分类所有属性、每个属性分组下所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }


    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
         relationService.saveBatch(vos);

        return R.ok();
    }
    /**
     * @description:  查询未关联基础属性
     * @param: [attrGroupId, params]
     * @return: com.ysz.common.utils.R
     * @author Yang
     * @date: 2023/9/25 14:30
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrGroupId,@RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.getNoRelationAttr(params,attrGroupId);
        return R.ok().put("page",pageUtils);
    }
    /**
     * @description:  属性分组关系删除
     * @param: [vos]
     * @return: com.ysz.common.utils.R
     * @author Yang
     * @date: 2023/9/25 11:44
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }
    ///product/attrgroup/{attrgroupId}/noattr/relation

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path =  categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
